//
//  ViewController3.m
//  核心动画代码
//
//  Created by 申元江 on 2018/7/30.
//  Copyright © 2018年 申元江. All rights reserved.
//

/**
  动画的原理： 设置初始值和结束值
 看到的都是假象，其实view的位置并没有改变
 动画时添加在layer
 坑：kCAFillModeForwoards 、 removedOnCompletion。否则动画被删除
 CALyer不处理用户交互
 
 隐式动画 和显式动画
 独立的layer是有隐式动画的，根layer是没有隐式动画的
 默认动画时间是0.25s
 
 
 */



#import "ViewController3.h"

@interface ViewController3 ()<CAAnimationDelegate>
@property (weak, nonatomic) IBOutlet UIView *blueView;

@property (nonatomic,strong) CALayer *layer;

@end

@implementation ViewController3

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    CALayer *layer = [CALayer layer];
    layer.frame = CGRectMake(100, 100, 100, 100);
    layer.backgroundColor = [UIColor yellowColor].CGColor;
    self.layer = layer;
    [self.view.layer addSublayer:self.layer];
    
    NSLog(@"---%@",NSStringFromCGRect(self.blueView.frame));
    NSLog(@"-----%@",NSStringFromCGRect(self.layer.frame));
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    CABasicAnimation *anim = [CABasicAnimation animation];
    anim.keyPath = @"position.y";
    anim.toValue = @400;
    anim.removedOnCompletion = NO;
    anim.fillMode = kCAFillModeForwards;
    anim.delegate = self;
    anim.duration = 1;
    [self.blueView.layer addAnimation:anim forKey:nil];
    [self.layer addAnimation:anim forKey:nil];
}

-(void)animationDidStart:(CAAnimation *)anim
{
    NSLog(@"begin");
}
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    NSLog(@"stop blueview--%@",NSStringFromCGRect(self.blueView.frame));
    NSLog(@"stop layer--%@",NSStringFromCGRect(self.layer.frame));
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
